from util import view, db, json, route, tpl

bp = route(__name__)

@bp.get("/")
async def home(request):
    for item in range(100000):
        pass
    return json({
        'msg': 'ok',
    })


@bp.get("/index")
@tpl('/index/index.html')
async def index(request):

    # 消息
    view.flash(request, "success message", "success")
    view.flash(request, "info message", "info")
    view.flash(request, "warning message", "warning")
    view.flash(request, "error message", "error")

    # session
    view.session(request)["user"] = "session 用户名"

    # 注解的 sql 返回的结果
    # rs = db.result.all(as_dict=True)
    rs = db.query('select * from moji limit 0,5').all(as_dict=True)

    return {
        'list': rs,
        'name': '首页'
    }

# 添加
@bp.get("/create")
async def create(request):
    return json({'index.py': 'create'})

# 编辑
@bp.get("/edit/<id:int>")
async def edit(request, id):
    params = {
        'id': id
    }
    row = db.query('select * from moji where cityId = :id', **params).one().as_dict()
    return json({
        'index.py': 'edit',
        'data': row
    })

# 详情页
@bp.get("/read/<id:int>")
async def read(request, id):
    params = {
        'id': id
    }
    row = db.query('select * from moji where cityId = :id', **params).one().as_dict()
    return json({
        'index.py': 'edit',
        'data': row
    })

# 保存
@bp.post("/save")
async def save(request):
    return json({'index.py': 'save'})

# 更新
@bp.post("/update/<id:int>")
async def update(request, id):
    return json({'index.py': 'edit', 'id': id})

# 删除
@bp.post("/delete/<id:int>")
async def save(request, id):
    return json({'index.py': 'delete'})